using UnityEngine;
using System.Collections;

public class DialogueDisplay: MonoBehaviour {

	public DialogueManager dmManager;
	public UIDisplay gameUI;

	public GameObject choicePanel;//--empty panel with UIGrid.cs
	public GameObject choiceObj;//--choiceUI prefab
	public string[] tmpChoice;

	public CameraManager camManager;
	public DialoguerDialogues _diaDia;

	void Awake()
	{
		gameUI = FindObjectOfType<UIDisplay> ();
		//dmManager = FindObjectOfType<DialogueManager> ();
		camManager = GameObject.Find ("CamManagerHolder").GetComponent<CameraManager> ();
		dmManager = GameObject.Find ("DialogueManager").GetComponent<DialogueManager> ();
	}
	// Use this for initialization
	void Start () {
		//dmManager.dm.getDialogueID ();	
		Dialoguer.Initialize ();
		dmManager.StartDialogue (_diaDia);

		Dialoguer.events.onStarted += OnStart;
		Dialoguer.events.onEnded += OnEnd;
		Dialoguer.events.onTextPhase += OnTextPhase;

		Dialoguer.events.onMessageEvent += HandleonMessageEvent;
	}

	void HandleonMessageEvent (string message, string metadata)
	{
		switch(message)
		{
			case "dialogueEnd":
			dialogueEnd(metadata);
			break;
			case "SceneChange":
			SceneChange(metadata);
			break;
			default:
			return;
		}
	}

	void dialogueEnd(string meta)
	{
		int i = int.Parse (meta);
		//print(metadata);
		camManager.changeCamera (i);
	}

	void SceneChange(string meta)
	{
		Application.LoadLevel(meta);
	}

	// Update is called once per frame
	void Update () {
		if (!dmManager.dm.getTextLabel ().activeSelf)
			dmManager.dm.getTextLabel ().SetActive (true);

		dmManager.UserInput ();
		dmManager = GameObject.Find ("DialogueManager").GetComponent<DialogueManager> ();
		if(Application.loadedLevelName == "PuzzleRoom")
		{
			camManager = GameObject.Find ("PuzzleRoomManager").GetComponent<CameraManager> ();
		}
//		//NOTE: Temp
//		if(Application.loadedLevelName == "PuzzleRoom")
//		{
//			dmManager = GameObject.Find("DialogueManager").GetComponent<DialogueManager>();
//			//dmManager.dm.setDialogueID(DialoguerDialogues.First_Puzzle_Items);
//		}
//		camManager = GameObject.Find ("CamManagerHolder").GetComponent<CameraManager> ();

	}

	public void showChoices()
	{
		dmManager.dm.setSkip (false);
		dmManager.dm.setAuto (false);	
		Transform[] trans = choicePanel.GetComponentsInChildren<Transform> ();
		for(int i=0; i<trans.Length; i++)
		{
			if(trans[i].gameObject.GetComponent<UIGrid>() == null)
				Destroy (trans[i].gameObject);
		}

		for(int i=0; i<dmManager.dm.getChoices().Length;i++)
		{
			GameObject tempChoice;
			tempChoice = Instantiate(choiceObj) as GameObject;

			tempChoice.transform.parent = choicePanel.transform;
			tempChoice.transform.localScale = Vector3.one;
			tempChoice.transform.localPosition = Vector3.zero;
				
			choicePanel.GetComponent<UIGrid>().repositionNow = true;
			print (i);
			tempChoice.GetComponentInChildren<UILabel>().text = dmManager.dm.getChoices()[i];

			tempChoice.name = i.ToString();
			tempChoice.GetComponentInChildren<UIButtonMessage>().target = GameObject.Find ("DialogueManager");
			tempChoice.GetComponentInChildren<UIButtonMessage>().functionName = "OnChoiceSelect";
		}

	}

	private void OnStart()
	{
		print ("start dialogue");
	}

	private void OnEnd()
	{
		dmManager.dm.getTextLabel ().SetActive (false);
		dmManager.dm.getTextLabel ().GetComponent<UILabel> ().text = "";
	
		//gameObject.GetComponent<CameraManager> ().changeCamera(gameObject.GetComponent<CameraManager>().camData.getCameraIndex());
		dmManager.dm.setHide (true);
	}

	private void OnTextPhase(DialoguerTextData data)
	{
		dmManager.dm.getTextLabel ().SetActive (false);
		dmManager.dm.setHide (false);
		if(dmManager.dm.getChoices().Length > 1)
		{
			showChoices();
		}
		else
		{
			//Will get alllll choices and destroy them
			Transform[] trans = choicePanel.GetComponentsInChildren<Transform> ();
			for(int i=0; i<trans.Length; i++)
			{
				if(trans[i].gameObject.GetComponent<UIGrid>() == null)
					Destroy (trans[i].gameObject);
			}
		}
		tmpChoice = dmManager.dm.getChoices ();
	}

	void OnLevelWasLoaded(int level)
	{
		if(level != 3)
			camManager = GameObject.Find ("CamManagerHolder").GetComponent<CameraManager> ();
	}
}
